//
//  UIButton+QSButton.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/20.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "UIButton+QSButton.h"



@implementation UIButton (QSButton)
//将按钮设置为图片在上，文字在下
- (void)setButtonTopImage:(UIButton *)sender{
    float  spacing = 5;//图片和文字的上下间距
    CGSize imageSize = sender.imageView.frame.size;
    CGSize titleSize = sender.titleLabel.frame.size;
    CGSize textSize = [sender.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : sender.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    sender.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    sender.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
}

- (void)setButtonRightImageWithButton:(UIButton *)sender{
    float  spacing = 5;//图片和文字的上下间距
    CGSize imageSize = sender.imageView.frame.size;
    CGSize titleSize = sender.titleLabel.frame.size;
    CGSize textSize = [sender.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : sender.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    
    sender.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width + 20, 0.0, 0.0);
    sender.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, imageSize.width);
}
@end
